import typeOfUtils from '@/utils/type-of-utils'
import htmlElementUtils from '@/utils/htmlElementUtils'

let visualDom = null

const textUtils = {
  /**
   * 文本长度计算
   * @param value
   * @param size
   * @param family
   * @return {number}
   */
  visualLength(value, size = 'inherit', family = 'inherit') {
    if (typeOfUtils.isEmpty(visualDom) || typeOfUtils.isNotHTMLDocument(visualDom)) {
      visualDom = document.createElement('span')
      htmlElementUtils.addChild(document.body, visualDom)
      visualDom.style.position = 'absolute'
      visualDom.style.visibility = 'hidden'
      visualDom.style.whiteSpace = 'nowrap'
      visualDom.style.zIndex = -100
    }
    visualDom.style.fontSize = size
    visualDom.style.fontFamily = family
    visualDom.innerHTML = value
    return visualDom.offsetWidth
  }
}

export default textUtils
